package com.letv.whatslive.model.convert;

import com.google.common.collect.Lists;
import com.letv.whatslive.common.utils.ObjectUtils;
import com.letv.whatslive.model.Employee;
import com.letv.whatslive.model.User;
import com.letv.whatslive.model.utils.ObjUtils;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by shenyue on 16-3-2.
 */
public class EmployeeConvert {

    public static DBObject castEmployeeToDBObject(Employee employee) {
        DBObject dbo = new BasicDBObject();
        if (employee.getId() != null) {
            dbo.put("_id", ObjUtils.toLong(employee.getId()));
        }
        if(StringUtils.isNotBlank(employee.getLoginName()))
            dbo.put("loginName", ObjUtils.toString(employee.getLoginName()));
        if(StringUtils.isNotBlank(employee.getDepartmentName()))
            dbo.put("departmentName", ObjUtils.toString(employee.getDepartmentName()));
        if(StringUtils.isNotBlank(employee.getPost()))
            dbo.put("post", ObjUtils.toString(employee.getPost()));
        dbo.put("createTime", ObjUtils.toLong(employee.getCreateTime(), System.currentTimeMillis()));
        dbo.put("updateTime", ObjUtils.toLong(employee.getUpdateTime(), System.currentTimeMillis()));
        if (employee.getDirectLeaderId() != null) {
            dbo.put("directLeaderId", ObjUtils.toLong(employee.getDirectLeaderId()));
        }
        if(employee.getIsHaveSuperPermission()!=null){
            dbo.put("isHaveSuperPermission", ObjUtils.toInteger(employee.getIsHaveSuperPermission()));
        }
        if(StringUtils.isNotBlank(employee.getName()))
            dbo.put("name", ObjUtils.toString(employee.getName()));
        if(StringUtils.isNotBlank(employee.getEmail()))
            dbo.put("email", ObjUtils.toString(employee.getEmail()));
        return dbo;
    }

    public static Employee castDBObjectToEmployee(DBObject dbObject){
        if(dbObject != null) {
            Employee employee = new Employee();
            employee.setId(ObjUtils.toLong(dbObject.get("_id")));
            employee.setCreateTime(ObjUtils.toLong(dbObject.get("createTime")));
            employee.setUpdateTime(ObjectUtils.toLong(dbObject.get("updateTime")));
            employee.setDirectLeaderId(ObjUtils.toLong(dbObject.get("directLeaderId")));
            employee.setLoginName(ObjectUtils.toString(dbObject.get("loginName")));
            employee.setDepartmentName(ObjectUtils.toString(dbObject.get("departmentName")));
            employee.setPost(ObjectUtils.toString(dbObject.get("post")));
            employee.setIsHaveSuperPermission(ObjectUtils.toInteger(dbObject.get("isHaveSuperPermission")));
            employee.setName(ObjectUtils.toString(dbObject.get("name")));
            employee.setEmail(ObjectUtils.toString(dbObject.get("email")));
            return employee;
        }else{
            return null;
        }

    }
}
